package mars;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  Eduardo Freire
 */
public class Main extends javax.swing.JFrame {
    private BufferedImage imagem = null;
    private File arquivo = null;
    private ImageFilter imageFilter = new ImageFilter();

  
    public Main() {
//        try {
////            UIManager.setLookAndFeel(new WindowsLookAndFeel());
//        }
//        catch(UnsupportedLookAndFeelException ex) {
//            ex.printStackTrace();
//        }
// 
        initComponents();
        setLocationRelativeTo(null);
    }

    public void abrir() throws Exception {
        JFileChooser escolha = new JFileChooser(".");
        escolha.showOpenDialog(null);
        arquivo = escolha.getSelectedFile();
        imagem = ImageIO.read(arquivo);
        if(imagem.getWidth() != imagem.getHeight()){
            JOptionPane.showMessageDialog(null, "A imagem deve ter dimensoes quadraticas\n");
            imagem = null;
            return;
        }

        imageFilter.setRaster(imagem.getRaster());

        jLabelImage.setIcon(new ImageIcon(imagem));

        jLabelImage.setSize(imagem.getWidth(), imagem.getHeight());
    }

    public void salvar() throws Exception {
        JFileChooser escolha = new JFileChooser("./");
        escolha.showSaveDialog(null);

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(escolha.getSelectedFile()));
        ImageIO.write(imagem, "bmp", out);
        JOptionPane.showMessageDialog(null, "Imagem Salva");
    }

    public void resetar() throws Exception {
        imagem = ImageIO.read(arquivo);
        imageFilter.setRaster(imagem.getRaster());
        jLabelImage.setIcon(new ImageIcon(imagem));
    }

    public void fDoG() throws Exception {
        imageFilter.FDoG(3, 5, false);
        jLabelImage.repaint();
    }

    public void fDoG_MIPS() throws Exception{
        imageFilter.FDoG(3, 5, true);
        jLabelImage.repaint();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jLabelImage = new javax.swing.JLabel();
        jMenuBar7 = new javax.swing.JMenuBar();
        jMenu13 = new javax.swing.JMenu();
        jMenuItemAbrir = new javax.swing.JMenuItem();
        jMenuItemSalvar = new javax.swing.JMenuItem();
        jMenu14 = new javax.swing.JMenu();
        jMenuItemResetar = new javax.swing.JMenuItem();
        jMenuItemFDoG = new javax.swing.JMenuItem();
        jMenuItemFDoGMIPS = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FDoG");

        jScrollPane1.setViewportView(jLabelImage);

        jMenu13.setText("File");

        jMenuItemAbrir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, 0));
        jMenuItemAbrir.setText("Abrir Imagem");
        jMenuItemAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAbrirActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItemAbrir);

        jMenuItemSalvar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, 0));
        jMenuItemSalvar.setText("Salvar");
        jMenuItemSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSalvarActionPerformed(evt);
            }
        });
        jMenu13.add(jMenuItemSalvar);

        jMenuBar7.add(jMenu13);

        jMenu14.setText("Edit");

        jMenuItemResetar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, 0));
        jMenuItemResetar.setText("Resetar Imagem");
        jMenuItemResetar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemResetarActionPerformed(evt);
            }
        });
        jMenu14.add(jMenuItemResetar);

        jMenuItemFDoG.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, 0));
        jMenuItemFDoG.setText("FDoG");
        jMenuItemFDoG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemFDoGActionPerformed(evt);
            }
        });
        jMenu14.add(jMenuItemFDoG);

        jMenuItemFDoGMIPS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, 0));
        jMenuItemFDoGMIPS.setText("FDoG MIPS");
        jMenuItemFDoGMIPS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemFDoGMIPSActionPerformed(evt);
            }
        });
        jMenu14.add(jMenuItemFDoGMIPS);

        jMenuBar7.add(jMenu14);

        setJMenuBar(jMenuBar7);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAbrirActionPerformed
        try {
            abrir();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
}//GEN-LAST:event_jMenuItemAbrirActionPerformed

    private void jMenuItemResetarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemResetarActionPerformed
        try {
            resetar();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
}//GEN-LAST:event_jMenuItemResetarActionPerformed

    private void jMenuItemSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSalvarActionPerformed
        try {
            salvar();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jMenuItemSalvarActionPerformed

    private void jMenuItemFDoGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemFDoGActionPerformed
        try {
            fDoG();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }

        
}//GEN-LAST:event_jMenuItemFDoGActionPerformed

    private void jMenuItemFDoGMIPSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemFDoGActionPerformed
        try {
            fDoG_MIPS();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JMenu jMenu13;
    private javax.swing.JMenu jMenu14;
    private javax.swing.JMenuBar jMenuBar7;
    private javax.swing.JMenuItem jMenuItemAbrir;
    private javax.swing.JMenuItem jMenuItemFDoG;
    private javax.swing.JMenuItem jMenuItemFDoGMIPS;
    private javax.swing.JMenuItem jMenuItemResetar;
    private javax.swing.JMenuItem jMenuItemSalvar;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
